package com.xunfang.portal.controller;

import com.xunfang.common.RedisConst;
import com.xunfang.common.enumeration.impl.BusinessCode;
import com.xunfang.common.exception.Assert;
import com.xunfang.portal.model.vo.CartVo;
import com.xunfang.portal.service.CartService;
import com.xunfang.portal.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author yellow Doctor
 * @date 2025-10-17
 * @desc https://blog.csdn.net/hekai7217
 */
@RestController
@RequestMapping("cart")
@Tag(name = "购物车的操作控制器")
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 查询购物车
     */
    @GetMapping
    @Operation(summary = "查询购物车")
    public ResponseEntity<CartVo> getCart(@RequestHeader(name = "Authorization", required = false) String token) {
        //校验Token
        Assert.error(!jwtUtils.validateToken(token), BusinessCode.INVALID_TOKEN);
        //查询用户ID
        String userId = jwtUtils.getValue(token, "userId");
        log.info("用户ID:{}", userId);
        //调用业务层查询购物车
        CartVo cart = cartService.getCart(Long.valueOf(userId));
        log.info("购物车信息:{}", cart);
        return ResponseEntity.ok(cart);
    }


    @PostMapping("/{bookId}")
    @Operation(summary = "添加商品的id到购物车中 ")
    public ResponseEntity<CartVo> addCartItem(
            @RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable("bookId") Long bookId
    ) {
        //查询用户ID
        String userId = jwtUtils.getValue(token, "userId");
        //调用业务层查询购物车
        CartVo cart = cartService.addCartItem(Long.valueOf(userId),bookId);
        return ResponseEntity.ok(cart);
    }

    @DeleteMapping("/{bookId}")
    @Operation(summary = "通过商品id 移除商品 ")
    public ResponseEntity<CartVo> removeCartItem(
            @RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable("bookId") Long bookId
    ) {
        //查询用户ID
        String userId = jwtUtils.getValue(token, "userId");
        //调用业务层  移除商品
        CartVo cart = cartService.removeCartItem(Long.valueOf(userId),bookId);
        return ResponseEntity.ok(cart);
    }

    @DeleteMapping("/clear")
    @Operation(summary = "清空购物车商品")
    public ResponseEntity<CartVo> clearCartItem(
            @RequestHeader(name = "Authorization", required = false) String token) {
        //查询用户ID
        String userId = jwtUtils.getValue(token, "userId");
        //调用业务层 清空商品
        CartVo cart = cartService.clearCartItem(Long.valueOf(userId));
        return ResponseEntity.ok(cart);
    }

    @PutMapping("/{bookId}/{count}")
    @Operation(summary = "修改商品数量")
    public ResponseEntity<CartVo> updateCartItem(
            @RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable("bookId") Long bookId,
            @PathVariable("count") Integer count
            ) {
        //查询用户ID
        String userId = jwtUtils.getValue(token, "userId");
        //调用业务层 更新商品数量
        CartVo cart = cartService.updateCartItemCount(
                Long.valueOf(userId),
                bookId,
                count
        );
        return ResponseEntity.ok(cart);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取我的购物车
     */
    @GetMapping("/myCartDetails")
    @Operation(summary = "获取我的购物车")
    public ResponseEntity<CartVo> myCartDetails(@RequestHeader(name = "Authorization", required = false) String token) {
        //获取用户ID
        String userId = jwtUtils.getValue(token, "userId");
        //从Redis中获取购物车的数据
        CartVo cart = (CartVo) redisTemplate.opsForValue().get(RedisConst.USER_CART_PREFIX + ":" + userId);
        return ResponseEntity.ok(cart);
    }
}
